package com.googlecode.tapestry5validator.hibernate.adapters;

import java.util.Map;

import org.apache.tapestry5.json.JSONObject;
import org.hibernate.validator.Range;

/**
 * Using for all validators, wich have check many, more than one,
 * parameters(example {@link Range} validator use two parameter length).
 * <p>
 * For single parameter using{@link ValueAdapter}.
 * <p>
 * All parameters transform to json add transferred to client javascript.
 * 
 * @see {@link ValueAdapter}
 * @author Valentine Yerastov
 */
public abstract class ParametersToJsonAdapter extends NotRequiredAdapter {

	@Override
	public String fetchToJSParametrs(Map<String, Object> parameters) {
		JSONObject json = new JSONObject();
		
		for (String key : parameters.keySet()) {
			json.put(key, parameters.get(key));
			if (key == "message") {
				throw new IllegalStateException(); 
			}
		}
		
		return json.toString();
	}
}
